package org.tea.inventory.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.tea.inventory.db.dao.AlumniWorkerWelfareMapper;
import org.tea.inventory.db.domain.AlumniWorkerWelfare;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AlumniWorkerWelfareService {

    @Resource
    private AlumniWorkerWelfareMapper welfareMapper;

    /**
     * 分页获取 员工福利 列表
     */
    public PageInfo<AlumniWorkerWelfare> querySelective(AlumniWorkerWelfare Welfare, int page, int limit, String sort) {
        PageHelper.startPage(page, limit);
        List<AlumniWorkerWelfare> list = welfareMapper.querySelective(Welfare);
        PageInfo<AlumniWorkerWelfare> pageInfo = new PageInfo<AlumniWorkerWelfare>(list);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        list.forEach(item->{
            item.setInUsableTime(true);
            if(item.getStartTime()!=null && sdf.format(date).compareTo(sdf.format(item.getStartTime())) < 0 ){
                item.setInUsableTime(false);
            }
            if(item.getEndTime()!=null && sdf.format(date).compareTo(sdf.format(item.getEndTime())) > 0 ){
                item.setInUsableTime(false);
            }
        });
        return pageInfo;
    }

    /**
     * 录入信息
     */
    public void add(AlumniWorkerWelfare welfare) {
        welfareMapper.insertSelective(welfare);
    }

    /**
     * 更新信息
     */
    public void update(AlumniWorkerWelfare welfare) {
        welfareMapper.updateByPrimaryKeySelective(welfare);
    }

    /**
     * 删除信息
     */
    public void delete(int id) {
        welfareMapper.deleteByPrimaryKey(id);
    }

    public void insertWelfareBatch(List<AlumniWorkerWelfare> listWelfare) {
        welfareMapper.insertWelfareBatch(listWelfare);
    }

    /**
     * 查看员工活动开始后是否发过中秋弹幕
     * @param employeeNo 员工号
     * @return 数量
     */
    public int isReasond(String employeeNo, Date startTime) {
        return welfareMapper.isReasond(employeeNo, startTime);
    }

    /**
     * 查询员工福利
     * @param welfare 封装条件
     * @return 员工福利
     */
    public AlumniWorkerWelfare querySelectiveOne(AlumniWorkerWelfare welfare) {
        List<AlumniWorkerWelfare> alumniWorkerWelfares = welfareMapper.querySelective(welfare);
        if (alumniWorkerWelfares != null && alumniWorkerWelfares.size() > 0) {
            return alumniWorkerWelfares.get(0);
        }
        return null;
    }
}
